class Schedule {
    Date start
    Date end
    String location
    Integer seats
    Course course
    Employee responsible
    static belongsTo = Course;
    static hasMany = [participations : Participations]

    static constraints = {
        start(nullable:false,validator: {
            val, obj ->
                if(val > obj.properties['end']) return "schedule.dates.customMismatch"
            }
        )
        location(size:2..20,blank:true,nullable:true,matches:"[a-öA-Ö0-9 ]+")
        seats(nullable:true,min:0,max:999)
        responsible(nullable:true)
    }

    public boolean constraintsOk(){
        //TODO : complete here: see with analysts what has to be done
        return true
    }

    public List<Employee> allParticipants(){
        ArrayList<Employee> p = new ArrayList<Employee>();
            for(Participations part in this.participations){
                Employee e = part.participant
                p.add(e);
            }
        return p;
    }

    public String toString(){
        return day() + " - From " + startHours() + " To " + endHours() + " ( "+location+")";
    }
    
    public String day(){
       Calendar cal = Calendar.getInstance();
       cal.setTime(start);
       return cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH);
    }

    public String startHours(){
       Calendar cal = Calendar.getInstance();
       cal.setTime(start);
       int hours = cal.get(Calendar.HOUR);
       int minutes = cal.get(Calendar.MINUTE);
       if(cal.get(Calendar.AM_PM)==Calendar.PM){
           hours+=12
       }
       return hours+":"+minutes;
    }
    public String endHours(){
       Calendar cal = Calendar.getInstance();
       cal.setTime(end);
       int hours = cal.get(Calendar.HOUR);
       int minutes = cal.get(Calendar.MINUTE);
       if(cal.get(Calendar.AM_PM)==Calendar.PM){
           hours+=12
       }
       return hours+":"+minutes;
    }

    public static List<Schedule> listNotApplied(Long empId,String courseId){
       boolean found=false
        List<Schedule> list = new ArrayList<Schedule>()

        def scheduleList = Schedule.findAllByCourse(Course.get(courseId))
       for(Schedule s in scheduleList){
           found=false
           for(Participations p in s.participations){
               if(p.participant.id==empId){
                   found=true
               }
           }
           if(!found){
               list.add(s)
           }
       }
    return list
    }

}

